package top.andnux.weather.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import java.util.List;

import top.andnux.weather.R;
import top.andnux.weather.adapter.WeatherAdapter;
import top.andnux.weather.controller.MainController;
import top.andnux.weather.databinding.FragmentHomeBinding;
import top.andnux.weather.model.MainModel;
import top.andnux.weather.view.MainView;

public class MainFragment extends BaseFragment<FragmentHomeBinding> implements MainView {

    private static final String TAG = "MainActivity";

    private MainController controller;
    private AlertDialog dialog;

    @Override
    protected FragmentHomeBinding createViewBinding(LayoutInflater inflater) {
        return FragmentHomeBinding.inflate(inflater);
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        controller = new MainController(context);
        controller.attachView(this);
        SharedPreferences preferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        String city = preferences.getString("city", "北京");
        binding.city.setText(city);
        controller.loadWeather(context, city);
        binding.city.setOnClickListener(v -> {
            showInputDialog(getContext(), "温馨提示",
                    binding.city.getText().toString(), "请输入城市名称？",
                    (dialog, content) -> {
                        controller.loadWeather(context, content);
                    });
        });
    }

    public interface OnEditInputClickListener {

        void onClick(DialogInterface dialog, String content);
    }

    public void showInputDialog(Context context, String title, String content, String hint, OnEditInputClickListener listener) {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
        final EditText editText = new EditText(context);
        editText.setText(content);
        editText.setHint(hint);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(context);
        inputDialog.setTitle(title).setView(editText);
        inputDialog.setNegativeButton(context.getString(R.string.cancel), (dialog, which) -> dialog.dismiss());
        dialog = inputDialog.setPositiveButton(context.getString(R.string.confirm), (dialog, which) -> {
            if (listener != null) {
                listener.onClick(dialog, editText.getText().toString());
            }
        }).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (controller != null) {
            controller.detachView();
            controller = null;
        }
    }

    @Override
    @SuppressWarnings("all")
    public void showMainView(MainModel model) {
        Log.d(TAG, "showMainView() called with: model = [" + model + "]");
        if (model.getErrorCode() == 0) {
            SharedPreferences preferences = context.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            preferences.edit().putString("city", model.getResult().getCity()).commit();
            binding.city.setText(model.getResult().getCity());
            MainModel.ResultDTO.RealtimeDTO realtime = model.getResult().getRealtime();
            binding.temperature.setText(realtime.getTemperature() + "℃");
            binding.info.setText(realtime.getInfo() + " " + realtime.getDirect() + " " + realtime.getPower());
            binding.aqi.setText("空气质量：" + realtime.getAqi());
            List<MainModel.ResultDTO.FutureDTO> future = model.getResult().getFuture();
            WeatherAdapter adapter = new WeatherAdapter(context, future);
            adapter.setCity(model.getResult().getCity());
            binding.content.setAdapter(adapter);
        }
    }
}